package com.xmut.mall.order.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmut.mall.order.searchparam.AfterSaleReasonSearchParam;
import com.xmut.mall.order.utils.PageUtil;
import com.xmut.mall.order.utils.RespBean;
import com.xmut.mall.order.vo.PageVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.order.utils.PageUtils;
import com.xmut.mall.order.utils.Query;

import com.xmut.mall.order.dao.AfterSaleReasonDao;
import com.xmut.mall.order.entity.AfterSaleReasonEntity;
import com.xmut.mall.order.service.AfterSaleReasonService;


@Service("afterSaleReasonService")
public class AfterSaleReasonServiceImpl extends ServiceImpl<AfterSaleReasonDao, AfterSaleReasonEntity> implements AfterSaleReasonService {


    /**
     * 获取售后原因分页数据
     * @param vo
     * @param params
     * @return
     */
    @Override
    public RespBean getAfterSaleReasonByPage(PageVO vo, AfterSaleReasonSearchParam params) {
        IPage<AfterSaleReasonEntity> page = this.baseMapper.getAfterSaleReasonByPage(PageUtil.initPage(vo),params);

        return RespBean.success("获取成功",page);
    }

    /**
     * 添加售后原因
     * @param params
     * @return
     */
    @Override
    public RespBean addAfterSaleReason(Map<String, Object> params) {
        String reason = MapUtil.getStr(params, "reason");
        String serviceType = MapUtil.getStr(params, "serviceType");

        AfterSaleReasonEntity entity = new AfterSaleReasonEntity();
        entity.setReason(reason);
        entity.setServiceType(serviceType);
        entity.setCreateTime(new Date());
        boolean save = this.save(entity);
        if (save){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }

    /**
     * 修改售后原因
     * @param params
     * @return
     */
    @Override
    public RespBean updateAfterSaleReason(Map<String, Object> params) {
        String reason = MapUtil.getStr(params, "reason");
        String serviceType = MapUtil.getStr(params, "serviceType");
        Long id = MapUtil.getLong(params, "id");

        boolean update = this.update(new UpdateWrapper<AfterSaleReasonEntity>()
                .set("reason", reason)
                .set("service_type", serviceType)
                .eq("id", id));
        if (update){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }

    }

    /**
     * 删除售后原因
     * @param id
     * @return
     */
    @Override
    public RespBean deleteAfterSaleReason(Long id) {
        boolean b = this.removeById(id);
        if (b){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }

    }

    /**
     * 根据售后类型获取官方设定的售后原因
     * @param type
     * @return
     */
    @Override
    public RespBean getAfterSaleReasonByType(String type) {
        List<AfterSaleReasonEntity> list = this.list(new QueryWrapper<AfterSaleReasonEntity>().eq("service_type", type));

        return RespBean.success("获取成功",list);

    }



}